<template>
  <!-- 登录和注册 -->
  <div class="loginView">
    <header>
      <div class="center">
        <router-link custom to="/my" v-slot="{ href, navigate }">
          <i class="iconfont icon-zuojiantou-copy"></i>
          <div :href="href" @click="navigate">登录</div>
        </router-link>
      </div>
    </header>

    <main>
      <van-tabs v-model:active="active" :animated="true">
        <van-tab title="手机验证码登录">
          <div class="center">
            <li>
              <a>手机号</a
              ><input
                autocomplete="off"
                type="text"
                @change="inputFun"
                data-name="text"
                :value="text"
                placeholder="手机号"
              />
            </li>
            <li>
              <a>验证码</a
              ><input
                autocomplete="off"
                type="text"
                @change="inputFun"
                data-name="code"
                :value="code"
                placeholder="验证码输入"
              />
              <span>获取验证码</span>
            </li>
            <button @click="btnemid">手机号登录</button>
            <p>
              <van-checkbox
                checked-color="#23b382"
                v-model="checked"
                icon-size="18px"
              />你已阅读并同意<span>《用户服务协议》</span>与<span
                >《隐私政策》</span
              >
            </p>
          </div>
        </van-tab>
        <van-tab title="账户密码登录">
          <div class="center">
            <van-cell-group inset>
              <li>
                <van-field
                  autocomplete="off"
                  v-model="tel"
                  type="tel"
                  placeholder="请输入手机号"
                  label="手机号"
                />
              </li>
              <li>
                <van-field
                  v-model="password"
                  type="password"
                  placeholder="请输入8~16密码"
                  label="密码"
                />
              </li>
            </van-cell-group>
            <button @click="inputFun">登录</button>
            <p>
              <van-checkbox
                checked-color="#23b382"
                v-model="checked"
                icon-size="18px"
              />你已阅读并同意<span>《用户服务协议》</span>与<span
                >《隐私政策》</span
              >
            </p>
          </div>
        </van-tab>
      </van-tabs>
    </main>
  </div>
</template>

<script>
import { ref } from "vue";
import { Toast } from "vant";
import { useRouter } from "vue-router";
export default {
  setup() {
    const router = useRouter();
    const active = ref(1);
    const checked = ref(false);
    const tel = ref("");
    const password = ref("");

    let text = ref("41");
    let code = ref("");

    let btnemid = () => {
      console.log(text);
      console.log(code);
    };
    let inputFun = () => {
      // 验证手机号
      let reg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      // 验证密码
      let pass = /^[a-zA-Z0-9]\w{8,17}$/;
      //  判断输入是否正确
      if (!reg.test(tel.value) || !pass.test(password.value)) {
        Toast.fail("请输入正确的手机号和密码");
        return;
      }
      if (!checked.value) {
        Toast.fail("请勾选阅读已同意");
        return;
      }
      let bookl = window.localStorage.getItem("booklD");
      let passt = window.localStorage.getItem("password");
      if (bookl || passt) {
        window.localStorage.setItem("booklD", tel.value);
        window.localStorage.setItem("password", password.value);
      } else {
        window.localStorage.setItem("booklD", tel.value);
        window.localStorage.setItem("password", password.value);
      }
      Toast.success("登录成功");
      router.push("./my");
    };

    return {
      btnemid, //手机号登录按钮
      inputFun,

      text, //手机号
      code, //验证
      active,
      tel,
      password,
      checked,
    };
  },
};
</script>

<style lang="scss" scoped>
.loginView {
  padding-top: 50px;
  header {
    padding: 14px 15px;
    border-bottom: 1px solid #f9f9f9;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    .center {
      color: #070707;
      width: 100%;
      position: relative;
      i {
        font-size: 18px;
      }
      div {
        top: 0;
        left: 0;
        text-align: center;
        width: 100%;
        position: absolute;
        font-size: 18px;
      }
    }
  }
  :deep {
    .van-cell van-field {
      display: inline-block;
    }
    .van-cell {
      padding: 0;
    }
    .van-checkbox {
      display: inline-block;
      margin-right: 10px;
      .van-checkbox__label {
        display: none;
      }
    }
    .van-tab {
      color: #9f9f9f;
    }
    .van-tab--active {
      color: #070707;
    }
    .van-tabs__line {
      display: none;
    }
  }
  main {
    padding: 0 26px;
    .center {
      padding-top: 10px;
      li {
        margin-top: 20px;
        display: flex;
        align-items: flex-end;
        padding-bottom: 5px;
        border-bottom: 2px solid #fafafa;
        a {
          margin-right: 10px;
          font-size: 14px;
          display: inline-block;
          color: #101010;
        }
        input {
          border: none;
          width: 50%;
        }
        span {
          margin-left: 25px;
          border-radius: 8px;
          font-size: 12px;
          padding: 5px 8px;
          display: inline-block;
          background-color: #21b382;
          color: #fff;
          border: 2px solid #0aa470;
        }
      }
      button {
        width: 100%;
        margin-top: 40px;
        padding: 11px 0;
        background-color: #23b382;
        border: 2px solid #0aa36f;
        color: #fff;
        border-radius: 8px;
      }
      p {
        line-height: 25px;
        margin-top: 25px;
        font-size: 14px;
        color: #acabad;
        span {
          color: #19a274;
        }
      }
    }
  }
}
</style>